const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId: 0,
    fileList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     //设置页面标题
     wx.setNavigationBarTitle({
      title: App.getConfig().team.photo
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin(),
    });
    if (_this.data.isLogin) {
      //获取服务队
      _this.setData({
        teamId: App.checkTeam()
      });
      // 获取数据
      this.getData();
    }
  },
  // 获取数据
  getData(e){
    let that = this;
    App._get('team/detail', {team_id:that.data.teamId}, result => {
      if(result.data.detail.images != false){
        that.setData({
          fileList: result.data.detail.images
        }); 
      }
    });
  },
    // 删除照片
    deleteClick(event) {
      var imgData = this.data.fileList;
      // 通过splice方法删除图片
      imgData.splice(event.detail.index, 1);
      console.log(event.detail)
      let that = this;
      App._get('upload/delImage', {
        fileIds:event.detail.file.image_id
      }, result => {

        // 更新图片数组
        this.setData({
          fileList: imgData
        }) 

      });

    },
    // 图片上传
    afterRead(event) {
      let that = this;//防止this指向问题
      //获取图片详细信息
      const {file} = event.detail;
      wx.showLoading({title: '上传中...'});
      //调用上传图片方法
      that.uploadCompressImg(file).then(res=>{
        console.log(res)
        App.uploadFile(res,{team_id:that.data.teamId},function(result){
          const fileList = that.data.fileList;
          // // 将图片信息添加到fileList数字中
          fileList.push({
              url: result.data.file_path,
              file_id:result.data.file_id,
          });
          // 更新存放图片的数组
          that.setData({
              fileList
          });
          wx.hideLoading();//停止loading
        })
      })
     
    },
    //大小超了
    oversize(event){
      App.showError("图片大小不能超过10M");
      return;
    },
  //图片压缩
  uploadCompressImg(file) {
    let that = this
    return new Promise(function(resolve, reject){
      let file_url = file.tempFilePath
      console.log(file);
      if(file.size < 500*1024){
        console.log("size",file.size)
        console.log("file_url",file_url)
        resolve(file_url);
      }else{
         // 获得原始图片大小
          wx.getImageInfo({
            src: file_url,
            success(res) {
              console.log(res);
            
              let originHeight = res.height;
              let originWidth = res.width;
              // 压缩比例
              // 最大尺寸限制
              let maxWidth = 1024,
                maxHeight = 1024;
              // 目标尺寸
              let targetWidth = originWidth,
                targetHeight = originHeight;
              // 等比例压缩，如果宽度大于高度，则宽度优先，否则高度优先
              if (originWidth > maxWidth || originHeight > maxHeight) {
                if (originWidth / originHeight > maxWidth / maxHeight) {
                  // 要求宽度*(原生图片比例)=新图片尺寸
                  targetWidth = maxWidth;
                  targetHeight = Math.round(maxWidth * (originHeight / originWidth));
                } else {
                  targetHeight = maxHeight;
                  targetWidth = Math.round(maxHeight * (originWidth / originHeight));
                }
              }

              // 更新 canvas 大小
              that.setData({
                "cw": targetWidth,
                "ch": targetHeight
              });
              // 尝试压缩文件，创建 canvas
              let ctx = wx.createCanvasContext('canvas');
              ctx.clearRect(0, 0, targetWidth, targetHeight);
              //通过得到图片旋转的角度来调整显示方向以正确显示图片，主要解决 ios 系统上的图片会有旋转的问题
              let orientation = res.orientation;
              if (orientation == 'right') { //exif的6
                that.setData({
                  "cw": targetHeight,
                  "ch": targetWidth
                });
                ctx.rotate(90 * Math.PI / 180);
                ctx.drawImage(file_url, 0, 0, targetWidth, targetHeight, 0, -targetHeight, that.data.ch, that.data.cw);
              } else {
                ctx.drawImage(file_url, 0, 0, targetWidth, targetHeight);
              }

              ctx.draw(false, function () {
                // 获得新图片输出
                wx.canvasToTempFilePath({
                  x: 0,
                  y: 0,
                  canvasId: 'canvas',
                  destWidth: targetWidth,
                  destHeight: targetHeight,
                  quality: 0.9,
                  fileType: "jpg",
                  success: (res) => {
                    console.log(res)
                    resolve(res.tempFilePath);
                  },
                  fail: (err) => {
                    console.error(err)
                  }
                }, this)
              })
            }
          })
      }
    })
  },
})